package com.itheima.health.service.impl;

import com.itheima.health.dao.ReportDao;
import com.itheima.health.dao.ReportOrderDao;
import com.itheima.health.entity.MemberReport;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import com.itheima.health.util.DateUtils;
import org.apache.poi.ss.formula.functions.Today;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 套餐预约占比业务层实现类
 *
 * @author 浅唱
 * @since 2022/11/22 0022 17:19
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;

    @Autowired
    private ReportOrderDao reportOrderDao;

    @Autowired
    private MemberReport memberReport;

    @Override
    public MemberReport getMemberReport() {
        Calendar date = Calendar.getInstance();
        List<String> months = this.getMonths(date);
        memberReport.setMonths(months);
        Calendar dateForSelect = Calendar.getInstance();
        dateForSelect.add(Calendar.MONTH, 1);
        List<String> monthsForSelect = this.getMonths(dateForSelect);
        List<Integer> monthCount = reportDao.selectByMonths(monthsForSelect);
        memberReport.setMemberCount(monthCount);
        return memberReport;
    }

    public List<String> getMonths(Calendar calendar) {
        List<String> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            String yearAndMonth = year + "-" + month;
            months.add(yearAndMonth);
            calendar.add(Calendar.MONTH, -1);
        }
        Collections.reverse(months);
        return months;
    }

    /**
     * 套餐预约占比饼形图
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return reportDao.findSetmealCount();
    }

    /**
     * 运营数据统计⻚⾯展示
     * <p>
     * Map数据格式：
     * 会员数据统计：
     * todayNewMember -> number  新增会员数
     * totalMember -> number 总会员数
     * thisWeekNewMember -> number 本周新增会员数
     * thisMonthNewMember -> number 本月新增会员数
     * <p>
     * 预约到诊数据统计：
     * todayOrderNumber -> number 今日预约数
     * thisWeekOrderNumber -> number 本周预约数
     * thisMonthOrderNumber -> number 本月预约数
     * <p>
     * todayVisitsNumber -> number 今日到诊数
     * thisWeekVisitsNumber -> number 本周到诊数
     * thisMonthVisitsNumber -> number 本月到诊数
     * <p>
     * 热门套餐：
     * hostSetmeals -> list<Setmeal> 热门套餐，取前四个
     */
    @Override
    public Map<String, Object> getBusinessReport() throws Exception {
/*        //获取今天日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());

        //获取本周周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

        //获取本月第一天
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        *//**
         * 会员数据统计
         *//*
        //新增会员数
        Integer todayNewMember = reportDao.findMemberountByToday(today);

        //总会员数
        Integer totalMember = reportDao.findAllMemberCount();

        //本周新增会员数
        Integer thisWeekNewMember = reportDao.findMemberAfterToday(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = reportDao.findMemberAfterToday(thisMonthFirstDay);


        *//**
         * 预约到诊数据统计
         * Order：预约
         * Visits：到诊
         *//*
        //今日预约数
        Integer todayOrderNumber = reportOrderDao.findOrderByToday(today);

        //本周预约数
        Integer thisWeekOrderNumber = reportOrderDao.findOrderAfterToday(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = reportOrderDao.findOrderAfterToday(thisMonthFirstDay);


        //今日到诊数
        Integer todayVisitsNumber = reportOrderDao.findVisitsBytoday(today);

        //本周到诊数
        Integer thisWeekVisitsNumber = reportOrderDao.findVisitsCountAfterToday(thisWeekMonday);

        //本月到诊数
        Integer thisMonthVisitsNumber = reportOrderDao.findVisitsCountAfterToday(thisMonthFirstDay);

        *//**
         * 热门套餐，查询前4条
         *//*
        List<Map> hostSetmeal = reportOrderDao.findHostSetmeal();


        Map<String, Object> ResultMap = new HashMap<>();

        //新增会员数
        ResultMap.put("todayNewMember", todayNewMember);
        //总会员数
        ResultMap.put("totalMember", totalMember);
        //本周新增会员数
        ResultMap.put("thisWeekNewMember", thisWeekNewMember);
        //本月新增会员数
        ResultMap.put("thisMonthNewMember", thisMonthNewMember);

        //今日预约数
        ResultMap.put("todayOrderNumber", todayOrderNumber);
        //本周预约数
        ResultMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
        //本月预约数
        ResultMap.put("thisMonthOrderNumber", thisMonthOrderNumber);

        //今日到诊数
        ResultMap.put("todayVisitsNumber", todayVisitsNumber);
        //本周到诊数
        ResultMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        //本月到诊数
        ResultMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

        //热门套餐，查询前4条
        ResultMap.put("hostSetmeal", hostSetmeal);

        return ResultMap;*/

        //获得当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天的日期
        String firstDay4ThisMonth =
                DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员数
        Integer todayNewMember = reportDao.findMemberCountByDate(today);

        //总会员数
        Integer totalMember = reportDao.findMemberTotalCount();

        //本周新增会员数
        Integer thisWeekNewMember = reportDao.findMemberCountAfterDate(thisWeekMonday);

        //本月新增会员数
        Integer thisMonthNewMember = reportDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = reportOrderDao.findOrderCountByDate(today);

        //本周预约数
        Integer thisWeekOrderNumber = reportOrderDao.findOrderCountAfterDate(thisWeekMonday);

        //本月预约数
        Integer thisMonthOrderNumber = reportOrderDao.findOrderCountAfterDate(firstDay4ThisMonth);

        //今日到诊数
        Integer todayVisitsNumber = reportOrderDao.findVisitsCountByDate(today);

        //本周到诊数
        Integer thisWeekVisitsNumber = reportOrderDao.findVisitsCountAfterDate(thisWeekMonday);

        //本月到诊数
        Integer thisMonthVisitsNumber = reportOrderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐（取前4）
        List<Map> hotSetmeal = reportOrderDao.findHotSetmeal();

        Map<String, Object> result = new HashMap<>();
        result.put("reportDate", today);
        result.put("todayNewMember", todayNewMember);
        result.put("totalMember", totalMember);
        result.put("thisWeekNewMember", thisWeekNewMember);
        result.put("thisMonthNewMember", thisMonthNewMember);
        result.put("todayOrderNumber", todayOrderNumber);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);
        result.put("todayVisitsNumber", todayVisitsNumber);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        result.put("hotSetmeal", hotSetmeal);

        return result;
    }
}
